/**
 * 安全奖品库存记录 列表页
 *
 * 作者 admin
 * 日期 2024-08-06 09:41:00
 */
layui.use(['jquery', 'laydate', 'febs', 'form', 'table', 'commonJS', 'validate'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        commonJS = layui.commonJS,
        validate = layui.validate,
        $view = $('#febs-baseGoodsInventoryRecord'),
        $query = $view.find('#query'),
        $searchForm = $view.find('#baseGoodsInventoryRecord-table-form'),
        tableIns;

    form.verify(validate);
    form.render();
    initDate();
    initTable();


    function initDate() {
        //初始化日期控件
        laydate.render({
            elem: '#laydate-range',
            // type: 'datetime',
            format: 'yyyy-MM-dd',
            range: ['#start-date', '#end-date'],
            btns:['now','confirm']
            // value: new Date(),
            // isInitValue: true
        })
        $('#end-date').val(commonJS.getCurrentDateStr(new Date()));
        $('#start-date').val(commonJS.getPastDateStr(new Date(),7));
        form.render();
    }

    function initTable() {
        tableIns = febs.table.init({
            elem: $view.find('table'),
            id: 'baseGoodsInventoryRecordTable',
            url: ctx + 'baseGoodsInventoryRecord/list',
            where: getQueryParams(),
            height: getTableHeight(),
            skin: '',
            cols: [[
                {type: 'checkbox'},
                {field: 'goodsInventoryRecordId', title: 'id', minWidth: 120, hide:true},
                {field: 'smallPath', title: '奖品图片', width: 100,align:'center',templet:function (d) {
                        var img = '<img class="tupian" style="width: 60px;height: 60px;"  src="' + ctx + d.smallPath + '" ' +
                            'data-bigsrc="' + ctx + d.filePath + '"  lay-event="imgEvent" /></img>';
                        return img;
                    }},
                {field: 'goodsName', title: '奖品名称', minWidth: 120},

                {field: 'operateType', title: '出入库类型', width: 120 ,align:'center',templet:function (d) {
                        if(d.operateType == '0'){
                            return  '<span class="layui-badge febs-tag-green">入库</span>';
                        }else {
                            return  '<span class="layui-badge febs-tag-blue">出库</span>';
                        }
                    }},
                {field: 'operateNumber', title: '数量',align:'center', width: 120},
                {field: 'beforeNumber', title: '操作前数量',align:'center', width: 120},
                {field: 'afterNumber', title: '操作后数量',align:'center', width: 120},
                {field: 'createdBy', title: '操作人',align:'center', width: 120},
                {field: 'createdTime', title: '操作时间',align:'center',width: 180},
            ]],
            toolbar: '#baseGoodsInventoryRecord-toolbar',
            defaultToolbar: []
        });
    }

    table.on('sort(baseGoodsInventoryRecordTable)', function (obj) {
        tableIns.reload({
            initSort: obj,
            where: $.extend(getQueryParams(), {
                field: obj.field,
                order: obj.type
            }),
            autoSort: true
        });
    });

    function reloadPage(curr) {
        var _curr = $view.find(".layui-laypage-em").next().html();
        if (!!curr) {
            _curr = curr;
        }
        tableIns.reload({where: getQueryParams(), page: {curr: _curr}});
    }

    $query.on('click', function () {
        reloadPage(1);
    });

    table.on('toolbar(baseGoodsInventoryRecordTable)', function (obj) {
        if (obj.event === 'addIuPut') {
            febs.modal.open('入库登记', 'baseGoodsInventoryRecord/add', {
                btn: ['提交','取消'],
                area: $(window).width() <= 750 ? '90%' : ['600px','500px'],
                yes: function (index, layero) {
                    $('#baseGoodsInventoryRecord-edit').find('#submit').trigger('click');
                },
                data:{
                    tenantId: $searchForm.find('select[name="tenantId"]').val(),
                    tenantName: $searchForm.find('select[name="tenantId"]').find('option:selected').text().trim(),
                    operateType: 0,
                }
            });
        }else if (obj.event === 'addOutPut') {
            febs.modal.open('出库登记', 'baseGoodsInventoryRecord/add', {
                btn: ['提交','取消'],
                area: $(window).width() <= 750 ? '90%' : ['600px','500px'],
                yes: function (index, layero) {
                    $('#baseGoodsInventoryRecord-edit').find('#submit').trigger('click');
                },
                data:{
                    tenantId: $searchForm.find('select[name="tenantId"]').val(),
                    tenantName: $searchForm.find('select[name="tenantId"]').find('option:selected').text().trim(),
                    operateType: 1,
                }
            });
        }
    });





    function getQueryParams() {
        return {
            goodsName: $searchForm.find('input[name="goodsName"]').val()?.trim(),
            startDate: $searchForm.find('input[name="startDate"]').val(),
            endDate: $searchForm.find('input[name="endDate"]').val(),
            tenantId: $searchForm.find("select[name='tenantId']").find("option:selected").val(),
            operateType: $searchForm.find("select[name='operateType']").find("option:selected").val(),
            invalidate_ie_cache: new Date()
        };
    }

    table.on('tool(baseGoodsInventoryRecordTable)', function (obj) {
        // console.log("event")
        var layEvent = obj.event;
        if (layEvent === 'imgEvent') {
            // console.log("event")
            $view.find(".big-img-div img").attr("src", ctx+obj.data.filePath);
        }
    });


    $view.find(".big-img-div img").load(function () {
        commonJS.showImgBig($view,this,this.width,this.height);
    });

    $view.find(".big-img-div").click(function () {//再次点击淡出消失弹出层
        $(this).fadeOut("fast");
    });

    function getTableHeight() {
        return commonJS.calcTableHeight();
    }

    $(window).resize(function () {
        tableIns.reload({height: getTableHeight()});
    });

});